package com.techindata.cacheredis;

import org.apache.log4j.Logger;
import redis.clients.jedis.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 张兵
 * Date: 2015/7/9
 * Time: 11:20
 * Email:390125214@qq.com
 * Description:【往缓存存入list类型的值,以及获取】
 */
public class CacheList {
    static Logger logger = Logger .getLogger(CacheList.class.getName());

    /**
     *  获取某一个key值下面数据,如果 start=0，end = -1表示全部
     * @param key 传入的key值
     * @param start 开始的索引数
     * @param end 结束的索引数
     * @return List<String> 类型
     */
    public static List<String> getLrange(String key,long start,long end){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.lrange(key,start,end);
        } catch (Exception e) {
//            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    /**
     *  在队列的头部插入数据
     * @param key
     * @param value
     * @return
     */
    public static long putHeadpush(String key,String ... value){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.lpush(key, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    /**
     * 队列中
     * @param key
     * @param value
     * @return
     */
    public static List<Object> lpushTran(String key,String ... value){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            Transaction tr = jedis.multi();
            tr.lpush(key,value);
            return tr.exec();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 像队列的尾部添加数据
     * @param key
     * @param value
     * @return
     */
    public static long putTailPush(String key,String ... value){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.rpush(key, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    /**
     * 在list类型中，通过key类获取对应的个数
     * @param key
     * @return
     */
    public static long getShortCacheLen(String key){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.llen(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return 0;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    /**
     * 将队列头部一条数据移出队列
     * @param key
     * @return
     */
    public static String getHeadPop(String key){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.lpop(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    /**
     * 将队列尾部一条数据移出队列
     * @param key
     * @return
     */
    public static String getTailPop(String key){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.rpop(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }


    /**
     *  移出count个value值
     * @param key key值
     * @param value value相等的值
     * @param count 个数
     *             
     * @return
     */
    public static long removeValue(String key,String value,int count){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.lrem(key,count,value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     *  统计key中value个数
     * @param key key值
     * @return
     */
    public static long llen(String key){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.llen(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }
    /**
     * 像队列里面插入一条数据
     * @param key 插入的key
     * @param where 插入方式，是在pivot之前还是之后
     * @param pivot 队列里面的某一个值
     * @param value 需要插入的值
     * @return
     */
    public static long insertCache(String key,BinaryClient.LIST_POSITION where, String pivot, String value){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.linsert(key,where,pivot,value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    public static long clearCache(String key){
        JedisPool pool = RedisConnectionPool.getPool();
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.del(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return -1;
        }
        finally{
            RedisConnectionPool.returnResource(pool, jedis);
        }
    }

    public static void main(String[] args) {
        boolean b = true;
        boolean isClean = false;
        while (!isClean){ //redis任务消费完全
            List<String> list = CacheList.getLrange("ddr", 0, -1);
            if(list.size()>0){
                for(String str : list){
                    String[] strs = str.split("\\|");
                    if("11".equals(strs[0])){
                        isClean = false;
                        break;
                    }else {
                        isClean = true;
                    }
                }
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                isClean = true;
            }
        }
        
    }
}
